<?php

namespace App\services;

class Pinyin
{
    public static function getChineseInitials($chinese): string
    {
        $pinyin = '';

        // 多音字姓氏映射数组
        $multiPinyinMap = [
            '单' => ['S', 'D'],
            '区' => ['O', 'Q'],
            '仇' => ['Q', 'C'],
            '解' => ['X', 'J'],
            '查' => ['Z', 'C'],
            '句' => ['G', 'J'],
            '阿' => ['E', 'A'],
            '瞿' => ['Q', 'J'],
            '阚' => ['K', 'H'],
            '乜' => ['N', 'M'],
            // 添加其他多音字姓氏的映射
        ];
        $chinese = mb_substr($chinese, 0, 1, 'UTF-8');


        if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $chinese)) {
            $str = mb_convert_encoding($chinese, 'gb2312', 'UTF-8');
            $asc = ord($str[0]) * 256 + ord($str[1]) - 65536;

            // 处理多音字姓氏
            $firstChar = mb_substr($chinese, 0, 1, 'UTF-8');
            if (isset($multiPinyinMap[$firstChar])) {
                // 选择第一个拼音作为姓氏的拼音
                $pinyin = $multiPinyinMap[$firstChar][0];
            } else {
                // 常规处理
                if ($asc >= -20319 && $asc <= -20284) {
                    $pinyin = 'A';
                } elseif ($asc >= -20283 && $asc <= -19776) {
                    $pinyin = 'B';
                } elseif ($asc >= -19775 && $asc <= -19219) {
                    $pinyin = 'C';
                } elseif ($asc >= -19218 && $asc <= -18711) {
                    $pinyin = 'D';
                } elseif ($asc >= -18710 && $asc <= -18527) {
                    $pinyin = 'E';
                } elseif ($asc >= -18526 && $asc <= -18240) {
                    $pinyin = 'F';
                } elseif ($asc >= -18239 && $asc <= -17923) {
                    $pinyin = 'G';
                } elseif ($asc >= -17922 && $asc <= -17418) {
                    $pinyin = 'H';
                } elseif ($asc >= -17417 && $asc <= -16475) {
                    $pinyin = 'J';
                } elseif ($asc >= -16474 && $asc <= -16213) {
                    $pinyin = 'K';
                } elseif ($asc >= -16212 && $asc <= -15641) {
                    $pinyin = 'L';
                } elseif ($asc >= -15640 && $asc <= -15166) {
                    $pinyin = 'M';
                } elseif ($asc >= -15165 && $asc <= -14923) {
                    $pinyin = 'N';
                } elseif ($asc >= -14922 && $asc <= -14915) {
                    $pinyin = 'O';
                } elseif ($asc >= -14914 && $asc <= -14631) {
                    $pinyin = 'P';
                } elseif ($asc >= -14630 && $asc <= -14150) {
                    $pinyin = 'Q';
                } elseif ($asc >= -14149 && $asc <= -14091) {
                    $pinyin = 'R';
                } elseif ($asc >= -14090 && $asc <= -13319) {
                    $pinyin = 'S';
                } elseif ($asc >= -13318 && $asc <= -12839) {
                    $pinyin = 'T';
                } elseif ($asc >= -12838 && $asc <= -12557) {
                    $pinyin = 'W';
                } elseif ($asc >= -12556 && $asc <= -11848) {
                    $pinyin = 'X';
                } elseif ($asc >= -11847 && $asc <= -11056) {
                    $pinyin = 'Y';
                } elseif ($asc >= -11055 && $asc <= -10247) {
                    $pinyin = 'Z';
                }
            }

            if($pinyin == '') {
                //百家姓中的生僻字
                $rare_arr = array(
                    -3652=>array('word'=>"窦",'first_char'=>'D'),
                    -8503=>array('word'=>"奚",'first_char'=>'X'),
                    -9286=>array('word'=>"酆",'first_char'=>'F'),
                    -7761=>array('word'=>"岑",'first_char'=>'C'),
                    -5128=>array('word'=>"滕",'first_char'=>'T'),
                    -9479=>array('word'=>"邬",'first_char'=>'W'),
                    -5456=>array('word'=>"臧",'first_char'=>'Z'),
                    -7223=>array('word'=>"闵",'first_char'=>'M'),
                    -2877=>array('word'=>"裘",'first_char'=>'Q'),
                    -6191=>array('word'=>"缪",'first_char'=>'M'),
                    -5414=>array('word'=>"贲",'first_char'=>'B'),
                    -4102=>array('word'=>"嵇",'first_char'=>'J'),
                    -8969=>array('word'=>"荀",'first_char'=>'X'),
                    -4938=>array('word'=>"於",'first_char'=>'Y'),
                    -9017=>array('word'=>"芮",'first_char'=>'R'),
                    -2848=>array('word'=>"羿",'first_char'=>'Y'),
                    -9477=>array('word'=>"邴",'first_char'=>'B'),
                    -9485=>array('word'=>"隗",'first_char'=>'K'),
                    -6731=>array('word'=>"宓",'first_char'=>'M'),
                    -9299=>array('word'=>"郗",'first_char'=>'X'),
                    -5905=>array('word'=>"栾",'first_char'=>'L'),
                    -4393=>array('word'=>"钭",'first_char'=>'T'),
                    -9300=>array('word'=>"郜",'first_char'=>'G'),
                    -8706=>array('word'=>"蔺",'first_char'=>'L'),
                    -3613=>array('word'=>"胥",'first_char'=>'X'),
                    -8777=>array('word'=>"莘",'first_char'=>'S'),
                    -6708=>array('word'=>"逄",'first_char'=>'P'),
                    -9302=>array('word'=>"郦",'first_char'=>'L'),
                    -5965=>array('word'=>"璩",'first_char'=>'Q'),
                    -6745=>array('word'=>"濮",'first_char'=>'P'),
                    -4888=>array('word'=>"扈",'first_char'=>'H'),
                    -9309=>array('word'=>"郏",'first_char'=>'J'),
                    -5428=>array('word'=>"晏",'first_char'=>'Y'),
                    -2849=>array('word'=>"暨",'first_char'=>'J'),
                    -7206=>array('word'=>"阙",'first_char'=>'Q'),
                    -4945=>array('word'=>"殳",'first_char'=>'S'),
                    -9753=>array('word'=>"夔",'first_char'=>'K'),
                    -10041=>array('word'=>"厍",'first_char'=>'S'),
                    -5429=>array('word'=>"晁",'first_char'=>'C'),
                    -2396=>array('word'=>"訾",'first_char'=>'Z'),
                    -7205=>array('word'=>"阚",'first_char'=>'K'),
                    -10049=>array('word'=>"乜",'first_char'=>'N'),
                    -10015=>array('word'=>"蒯",'first_char'=>'K'),
                    -3133=>array('word'=>"竺",'first_char'=>'Z'),
                    -6698=>array('word'=>"逯",'first_char'=>'L'),
                    -9799=>array('word'=>"俟",'first_char'=>'Q'),
                    -6749=>array('word'=>"澹",'first_char'=>'T'),
                    -7220=>array('word'=>"闾",'first_char'=>'L'),
                    -10047=>array('word'=>"亓",'first_char'=>'Q'),
                    -10005=>array('word'=>"仉",'first_char'=>'Z'),
                    -3417=>array('word'=>"颛",'first_char'=>'Z'),
                    -6431=>array('word'=>"驷",'first_char'=>'S'),
                    -7226=>array('word'=>"闫",'first_char'=>'Y'),
                    -9293=>array('word'=>"鄢",'first_char'=>'Y'),
                    -6205=>array('word'=>"缑",'first_char'=>'G'),
                    -9764=>array('word'=>"佘",'first_char'=>'S'),
                    -9818=>array('word'=>"佴",'first_char'=>'N'),
                    -9509=>array('word'=>"谯",'first_char'=>'Q'),
                    -3122=>array('word'=>"笪",'first_char'=>'D'),
                    -9823=>array('word'=>"佟",'first_char'=>'T'),
                );
                if(array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']){
                    return $rare_arr[$asc]['first_char'] ;
                }else{
                    return '*';
                }
            }
        }
        return $pinyin;

    }

}
